//
//  QuestionModel.swift
//  SwiftDemo
//
//  Created by DYD on 2018/12/5.
//  Copyright © 2018 DYD. All rights reserved.
//

import UIKit
import ObjectMapper
import RxSwift

let IndexNone = 10086

class QuestionItemModel: NSObject, Mappable {
    
    var name = ""
    var selected = false {
        willSet {
            selectVariable.value = newValue
        }
    }
    var selectVariable = Variable(false)
    
    // 上传用
    var uploadInfo = ""
    var uploadDetail = ""
    
    override init() {
        
    }
    
    init(applyInfo: ApplyChooseModel) {
        name = applyInfo.name
        uploadDetail = "\(applyInfo.id)"
    }
    
    required init?(map: Map) {
        
    }
    
    func mapping(map: Map) {
        name <- map["name"]
        uploadInfo <- map["uploadInfo"]
        uploadDetail <- map["uploadDetail"]
        selected <- map["selected"]
    }
    
}


class QuestionModel: NSObject, Mappable {
    
    override init() {
        
    }
    
    func mapping(map: Map) {
        multiChoose <- map["multiChoose"]
        canEnterSupply <- map["canEnterSupply"]
        supplyTip <- map["supplyTip"]
        questionTitle <- map["questionTitle"]
        itemScale <- map["itemScale"]
        itemHeight <- map["itemHeight"]
        items <- map["items"]
        viewHeight <- map["viewHeight"]
        selectedIndex <- map["selectedIndex"]
        enterInfo <- map["enterInfo"]
        enterDetail <- map["enterDetail"]
    }
    
    
    var itemSize: CGSize {
        get {
            var width = screenWidth
            if itemScale >= 1 {
                width = screenWidth / CGFloat(itemScale)
            }
            return CGSize(width: width, height: itemHeight)
        }
    }
    
    var multiChoose = false
    var canEnterSupply = false
    var supplyTip = ""
    var questionTitle = ""
    var itemScale:CGFloat = 1.0
    var itemHeight:CGFloat = 30.0
    var items = [QuestionItemModel]()
    var viewHeight: CGFloat = 0.0
    
    // 上传用
    var enterInfo = ""
    var enterDetail = ""
    
    var selectedIndex: [Int] {
        set {
            let new = newValue.filter { (obj) -> Bool in
                obj != IndexNone
            }
            if !multiChoose {
                
                if (new.isEmpty || new.first! >= items.count) {
                    return
                }
                for (index,model) in items.enumerated() {
                    model.selected = new.first! == index
                }
                return
            }
            for model in items {
                model.selected = false
            }
            for i in new {
                if (i >= items.count) {
                    return
                }
                for (index,model) in items.enumerated() {
                    if i == index {
                        model.selected = true
                        model.uploadDetail = "1"
                    }
                }
            }
        }
        get {
            if multiChoose {
                var tmp = [Int]()
                for (index,model) in items.enumerated() {
                    if (model.selected) {
                        tmp += [index]
                    }
                }
                return tmp
            } else {
                for (index,model) in items.enumerated() {
                    if (model.selected) {
                        return [index]
                    }
                }
                return [IndexNone]
            }
        }
    }
    
    required init?(map: Map) {
        
    }
    
    
}
